package com.cskaoyan.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author stone
 * @date 2022/05/25 11:23
 */
@Component
public class LifeCycleBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware,
        InitializingBean, DisposableBean {
    public LifeCycleBean() {
        System.out.println("Bean的实例化");
    }

    @Autowired
    User user;

    String beanName;
    BeanFactory beanFactory;
    ApplicationContext applicationContext;
    @Override
    public void setBeanName(String beanName) {
        System.out.println("beanName = " + beanName);
        this.beanName = beanName;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("beanFactory = " + beanFactory);
        this.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("applicationContext = " + applicationContext);
        this.applicationContext = applicationContext;
    }

    // 方法名可以任意写
    @PostConstruct
    public void init() {
        System.out.println("自定义init");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean的afterPropertiesSet方法");
    }

    @PreDestroy
    public void customDestroy() {
        System.out.println("自定义destroy");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean提供的destroy");
    }
}
